/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace DaveSexton.Sandcastle
{
	partial class ResolveExternalLinksComponentHost : IServiceProvider
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		private readonly IServiceProvider provider;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ResolveExternalLinksComponentHost" /> class for derived classes.
		/// </summary>
		protected ResolveExternalLinksComponentHost(IServiceProvider provider)
		{
			this.provider = provider;
		}
		#endregion

		#region Methods
		/// <summary>
		/// Shows the specified <paramref name="form"/> as a modal dialog with the host's main form as its 
		/// parent, if the <see cref="IUIService"/> service is available, and returns the 
		/// <see cref="DialogResult"/>.  If the service is unavailable then <see cref="Form.ShowDialog"/>
		/// is called without specifying an owner.
		/// </summary>
		public DialogResult ShowDialog(Form form)
		{
			return ShowDialog(form, this);
		}

		/// <summary>
		/// Shows the specified <paramref name="form"/> as a modal dialog with the specified <paramref name="host"/>'s 
		/// main form as its parent, if the <see cref="IUIService"/> service is available, and returns the 
		/// <see cref="DialogResult"/>.  If the service is unavailable then <see cref="Form.ShowDialog"/> is called 
		/// without specifying an owner.
		/// </summary>
		public static DialogResult ShowDialog(Form form, ResolveExternalLinksComponentHost host)
		{
			if (form == null)
				throw new ArgumentNullException("form");

			IUIService hostUI = (host == null) ? null : host.GetService(typeof(IUIService)) as IUIService;

			return ((hostUI == null) ? form.ShowDialog() : hostUI.ShowDialog(form));
		}
		#endregion

		#region IServiceProvider Members
		/// <summary>
		/// Gets the service object of the specified type.
		/// </summary>
		public object GetService(Type serviceType)
		{
			if (provider == null)
				return null;
			else
				return provider.GetService(serviceType);
		}
		#endregion
	}
}
